package com.tang.test;

import com.alibaba.fastjson.JSON;
import com.tang.client.ClientSocket;
import com.tang.future.SyncWrite;
import com.tang.msg.Request;
import com.tang.msg.Response;
import io.netty.channel.ChannelFuture;
import lombok.extern.slf4j.Slf4j;

/**
 * 启动client
 *
 * @author fulin.tang
 * @version V3.0
 * @since 2022/5/19 18:16
 */
@Slf4j
public class StartClient {

    private static ChannelFuture future;
    
    public static void main(String[] args) {
        log.info("hi");
        ClientSocket clientSocket = new ClientSocket();
        new Thread(clientSocket).start();
        
        while (true) {
            try {
                if (null == future) {
                    future = clientSocket.getChannelFuture();
                    Thread.sleep(500);
                    continue;
                }
                // 构建发送参数
                Request request = new Request();
                request.setResult("查询用户信息");
                SyncWrite syncWrite = new SyncWrite();
                Response response = syncWrite.writeAndSync(future.channel(), request, 1000);
                log.info("调用结果：{}", JSON.toJSON(response));
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
}
